using System;
using System.Diagnostics;

using System.IO;
using System.IO.Compression;

namespace NBox.Service.Compression
{
    /// <summary>
    /// Compress Manager
    /// </summary>
    public sealed class CompressionManager
    {
        private static IConfigurationReader _configurationReader = new ConfigurationReader();
        private static object _loadLock = new object();
        private static readonly string COMMON_SECTION_COMPRESSION = "nbox.service/compression";

        private CompressionManager()
        {
        }

        private static ICompressor BuildCompressorFactory()
        {
            CompressionSetting section = null;
            try
            {
                section = (CompressionSetting)ConfigurationReader.GetSection(COMMON_SECTION_COMPRESSION);
            }
            catch (Exception exception)
            {
                throw new ConfigurationException("Could not configure NBox.Service.Compression from configuration section 'nbox.service/compression'.", exception);
            }
            if ((section != null) && !typeof(ICompressor).IsAssignableFrom(section.CompressorType))
            {
                throw new ConfigurationException("Specified Compressor does not implement ICompressor.  Check implementation of class " + section.CompressorType.AssemblyQualifiedName);
            }
            ICompressor compressor = null;
            if (section != null)
            {
                try
                {
                    if (section.Properties.Count > 0)
                    {
                        object[] args = new object[] { section.Properties };
                        compressor = (ICompressor)Activator.CreateInstance(section.CompressorType, args);
                    }
                    else
                    {
                        compressor = (ICompressor)Activator.CreateInstance(section.CompressorType);
                    }
                }
                catch (Exception exception2)
                {
                    throw new ConfigurationException("Unable to create instance of type " + section.CompressorType.FullName + ". Possible explanation is lack of zero arg and single arg NameValueCollection constructors", exception2);
                }
                return compressor;
            }

            ICompressor compressor2 = new NBox.Service.Compression.GZipCompressor();
            Trace.WriteLine("Unable to read configuration section nbox.service/compression.  Using GZip implemenation.");
            return compressor2;
        }

        public static ICompressor GetCompressor()
        {
            return BuildCompressorFactory();
        }

        public static ICompressor GetCompressor(string name)
        {
            return BuildCompressorFactory();
        }

        public static IConfigurationReader ConfigurationReader
        {
            get
            {
                return _configurationReader;
            }
            set
            {
                _configurationReader = value;
            }
        }
    }
}
